
// home//pages/index_vip1/index.js
const apis = require('../../../utils/api').apis;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList:[],
    dataArr:[],
    searchForm:{
      time:'',
      price:'',
      purchaseNum:'',
      pageNo:1,
      pageSize:10,
    },
    total:0,
    activeIndex:-1,
    userType:'user'
  },
  searchWord() {
    wx.navigateTo({
      url: '/home/pages/search/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userinfo=wx.getStorageSync('userInfo')
    this.setData({
      userType:userinfo.userType
    })
    apis.bannerQueryBanner({
      relationType:'vip'
    }).then(res=>{
      this.setData({
        bannerList:res.data
      })
    })
    this.getData()
  },
  goDetail(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/curriculum/pages/course/index?id='+id,
    })
  },
  getData(){
    wx.showLoading({
      title: '加载中',
    })
    apis.curriculumQueryVipCurriculum({
      time:this.data.searchForm.time,
      price:this.data.searchForm.price,
      purchaseNum:this.data.searchForm.purchaseNum,
      pageNo:this.data.searchForm.pageNo,
      pageSize:this.data.searchForm.pageSize
    }).then(res=>{
      if(this.data.searchForm.pageNo==1){
        this.setData({
          dataArr:res.data.records,
          total:res.data.total
        })
      }else{
        this.setData({
          dataArr:this.data.dataArr.concat(res.data.records),
          total:res.data.total
        })
      }
      wx.hideLoading()
      // console.log(this.data.total,this.data.dataArr)
    })
  },
  changInd(e) {
    // console.log(e.currentTarget.dataset.ind);
    this.setData({
      dataArr:[]
    })
    let type=e.currentTarget.dataset.type;
    let ind=e.currentTarget.dataset.ind;
    if (this.data.activeIndex == -1) {
      this.setData({
        activeIndex: e.currentTarget.dataset.ind
      })
    } else {
      this.setData({
        activeIndex: -1
      })
    }
    if(type=='time'){
        this.setData({
          'searchForm.time':this.data.searchForm.time==''||this.data.searchForm.time=='down'?'up':'down',
          'searchForm.price':'',
          'searchForm.purchaseNum':''
        })
    }else if(type=='purchaseNum'){
      this.setData({
        'searchForm.purchaseNum':this.data.searchForm.purchaseNum==''||this.data.searchForm.purchaseNum=='down'?'up':'down',
        'searchForm.price':'',
        'searchForm.time':''
      })
    }else if(type=='price'){
      this.setData({
        'searchForm.price':this.data.searchForm.price==''||this.data.searchForm.price=='down'?'up':'down',
        'searchForm.purchaseNum':'',
        'searchForm.time':''
      })
    }
    console.log(this.data.activeIndex)
    this.getData()
  },
  goVip(){
    wx.navigateTo({
      url: '/vip/pages/vip_buy/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageNo:1
    })
    this.getData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.total,this.data.dataArr.length)

    if(this.data.total>this.data.dataArr.length){

      this.setData({
        pageNo: ++this.data.searchForm.pageNo
      })
      console.log(this.data.searchForm.pageNo)
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})